package com.hll.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hll.api.config.web.RespMessage;
import com.hll.api.entity.Admin;
import com.hll.api.exception.MyException;
import com.hll.api.mapper.AdminMapper;
import com.hll.api.service.IAdminService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 管理员服务实现类
 * </p>
 *
 * @author 何龙
 * @since 2019-07-18
 */
@Service("xinhuaAdminServiceImpl")
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

	final AdminMapper mapper;

	public AdminServiceImpl(AdminMapper mapper) {this.mapper = mapper;}

	@Override
	public Admin getXinuhaAdmin(HttpSession session) {
		Object id = session.getAttribute(Admin.ADMIN_SESSION);
		if (id == null) {
			throw new MyException(RespMessage.Status.NON_PERMISSION, "登录失效,请重新登录");
		}
		Admin admin = mapper.selectById((Integer) id);
		if (admin == null) {
			throw new MyException(RespMessage.Status.NON_PERMISSION, "用户不存在");
		}
		return admin;
	}

	@Override
	public Admin findByName(String name) {
		QueryWrapper<Admin> wrapper = new QueryWrapper<>();
		wrapper.eq("name",name);
		return mapper.selectOne(wrapper);
	}
}
